package com.boot2.core.utils;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author zhangweilin
 * @Description: xxxxx
 * @date 2020/6/23
 */
public class LocalDateTimeUtil {

    private static final DateTimeFormatter NORMAL_DATE_TIME_FORMATTER =
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault());
    private static final DateTimeFormatter SIMPLE_DATE_TIME_FORMATTER =
            DateTimeFormatter.ofPattern("yyyyMMddHHmmss").withZone(ZoneId.systemDefault());

    private static final DateTimeFormatter DB_DATE_TIME_FORMATTER =
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS").withZone(ZoneId.systemDefault());

    private static final DateTimeFormatter SIMPLE_DATE_HOUR_MINUTE_TIME_FORMATTER =
            DateTimeFormatter.ofPattern("yyyyMMddHHmm").withZone(ZoneId.systemDefault());
    private static final DateTimeFormatter SIMPLE_DATE_TIME_HOUR_FORMATTER
            = DateTimeFormatter.ofPattern(
            "yyyyMMddHH").withZone(ZoneId.systemDefault());

    private static final DateTimeFormatter NORMAL_DATE_FORMATTER =
            DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneId.systemDefault());
    private static final DateTimeFormatter SIMPLE_DATE_FORMATTER =
            DateTimeFormatter.ofPattern("yyyyMMdd").withZone(ZoneId.systemDefault());

    private static final DateTimeFormatter SMS_REMINDER_DATE_FORMATTER =
            DateTimeFormatter.ofPattern("yyyy年MM月dd日").withZone(ZoneId.systemDefault());

    private static final DateTimeFormatter CTRL_LOG_TIME_FORMATTER =
            DateTimeFormatter.ofPattern("MM-dd HH:mm").withZone(ZoneId.systemDefault());


    /**
     * 日期时间格式化 yyyy-MM-dd HH:mm:ss
     *
     * @param dateTime
     * @return
     */
    public static String localDateTimeFormat(LocalDateTime dateTime) {
        return dateTime != null ? dateTime.format(NORMAL_DATE_TIME_FORMATTER) : "";
    }

    /**
     * @param dateTime
     * @return
     */
    public static String localDateTimeDbFormat(LocalDateTime dateTime) {
        return dateTime != null ? dateTime.format(NORMAL_DATE_TIME_FORMATTER) : null;
    }

    public static String localDateTimeDbFormat2(LocalDateTime dateTime) {
        return dateTime != null ? dateTime.format(DB_DATE_TIME_FORMATTER) : null;
    }

    /**
     * 日期格式化 yyyy-MM-dd
     *
     * @param date
     * @return
     */
    public static String localDateFormat(LocalDate date) {
        return date != null ? date.format(NORMAL_DATE_FORMATTER) : "";
    }

    /**
     * 日期时间字符串转LocalDateTime(yyyy-MM-dd HH:mm:ss)
     *
     * @param localDateTime
     * @return
     */
    public static LocalDateTime str2LocalDateTime(String localDateTime) {
        if (StringUtils.isBlank(localDateTime)) {
            return null;
        }
        return LocalDateTime.parse(localDateTime, NORMAL_DATE_TIME_FORMATTER);
    }

    /**
     * 日期时间字符串转LocalDateTime(yyyyMMddHHmmss)
     *
     * @param localDateTime
     * @return
     */
    public static LocalDateTime simpleStr2LocalDateTime(String localDateTime) {
        if (StringUtils.isBlank(localDateTime)) {
            return null;
        }
        return LocalDateTime.parse(localDateTime, SIMPLE_DATE_TIME_FORMATTER);
    }

    /**
     * LocalDateTime(yyyyMMddHHmmss)转日期时间字符串
     *
     * @param localDateTime
     * @return
     */
    public static String localDateTime2SimpleStr(LocalDateTime localDateTime) {
        if (null==localDateTime) {
            return null;
        }
        return localDateTime.format(SIMPLE_DATE_TIME_FORMATTER);
    }

    /**
     * @param timestamp
     * @return
     */
    public static LocalDateTime getLocalDateTimeFromTimestamp(Timestamp timestamp) {
        if (timestamp == null || timestamp.getTime() <= 0) {
            return null;
        }
        return LocalDateTime.ofInstant(
                Instant.ofEpochMilli(timestamp.getTime()), TimeZone.getDefault().toZoneId());
    }

    /**
     * @param timestamp
     * @return
     */
    public static LocalDate getLocalDateFromTimestamp(Timestamp timestamp) {
        if (timestamp == null || timestamp.getTime() <= 0) {
            return null;
        }
        return LocalDateTime.ofInstant(
                Instant.ofEpochMilli(timestamp.getTime()), TimeZone.getDefault().toZoneId()).toLocalDate();
    }

    /**
     * Date转换为LocalDateTime
     *
     * @param date
     */
    public static LocalDateTime date2LocalDateTime(Date date) {
        Instant instant = date.toInstant();//An instantaneous point on the time-line.(时间线上的一个瞬时点。)
        ZoneId zoneId = ZoneId.systemDefault();//A time-zone ID, such as {@code Europe/Paris}.(时区)
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        return localDateTime;
    }

    /**
     * LocalDateTime转换为Date
     *
     * @param localDateTime
     */
    public static Date localDateTime2Date(LocalDateTime localDateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);//Combines this date-time with a time-zone to create a  ZonedDateTime.
        Date date = Date.from(zdt.toInstant());
        return date;
    }

}
